﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WindowsFormsApplication1
{
    public class Atoi
    {
        public static int atoi(string number)
        {
            int returnValue = 0;

            bool isNegative = false;
            int startPosition = 0;

            if (number[0] == '-')
            {
                isNegative = true;
                startPosition = 1;
            }

            for (int i = startPosition; i < number.Length; i++)
            {
                if ((number[i] >= '0') && number[i] <= '9')
                {
                    int currentInteger = number[i] - '0';
                    if (i > 0) returnValue = returnValue * 10;
                    returnValue = returnValue + currentInteger;
                }
            }
            return isNegative ? -1 * returnValue : returnValue;
        }
    }
}
